package com.ruoyi.algorithm.controller;

import com.ruoyi.algorithm.entity.Operator;
import com.ruoyi.algorithm.entity.TableStruct;
import com.ruoyi.algorithm.model.OperatorModel;
import com.ruoyi.algorithm.model.OperatorOnLineStateModel;
import com.ruoyi.algorithm.service.OperatorService;
import com.ruoyi.algorithm.service.TableStructService;
import com.beihu.sightplus.common.authorization.entity.User;
import com.beihu.sightplus.common.authorization.service.UserAuthorization;
import com.beihu.sightplus.common.request.PageRequest;
import com.beihu.sightplus.common.response.PageAndConditionResult;
import com.beihu.sightplus.common.response.PageResult;
import com.beihu.sightplus.common.response.ResultData;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/operator")
@Api(value = "算子API", tags = {"算子API"})
public class OperatorController {

    @Autowired
    private OperatorService operatorService;

    @Autowired
    private TableStructService tableStructService;

    /**
     * 新增
     */
    @ApiOperation("新增")
    @PostMapping("/add")
    public ResultData add(@RequestBody Operator operator){
        operatorService.add(operator);
        return ResultData.success();
    }

    /**
     * 修改
     */
    @ApiOperation("修改")
    @PostMapping("/update")
    public void update(@RequestBody Operator operator) {
        operatorService.update(operator);
    }

    /**
     * 删除
     */
    @ApiOperation("删除")
    @ApiImplicitParam(name = "operatorId", value = "算子Id", required = true)
    @DeleteMapping("/delete/{operatorId}")
    public ResultData delete(@PathVariable Long operatorId) {
        int deleteCount = operatorService.delete(operatorId);
        if (deleteCount == 0){
            return ResultData.success();
        } else {
            return new ResultData(-1, "因该算子被算子版本绑定，所以无法删除", null);
        }
    }

    /**
     * 上下线
     */
    @ApiOperation("上下线")
    @PostMapping("/setOnlineState")
    public ResultData setOnlineState(@RequestBody OperatorOnLineStateModel operatorOnLineStateModel) {
        operatorService.setOnlineState(operatorOnLineStateModel);
        return ResultData.success();
    }

    /**
     * 查询算子详情信息
     */
    @ApiOperation("查询算子详情信息")
    @ApiImplicitParam(name = "operatorId", value = "算子Id", required = true)
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功", response = Operator.class)
    })
    @GetMapping("/detail/{operatorId}")
    public ResultData detail(@PathVariable Long operatorId) {
        return ResultData.success(operatorService.detail(operatorId));
    }

    /**
     * 查询厂商和算子信息，支持根据算子名称查询
     */
    @ApiOperation("查询厂商和算子信息，支持根据算子名称查询")
    @ApiImplicitParam(name = "operatorName", value = "算子名称", required = true)
    @GetMapping("/getManufacturerOperatorDDB")
    public ResultData getManufacturerOperatorDDB(String operatorName) {
        return ResultData.success(operatorService.getManufacturerOperatorDDB(operatorName));
    }

    /**
     * 分页操作
     */
    @ApiOperation("分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderAttr", value = "需要排序的属性"),
            @ApiImplicitParam(name = "orderType", value = "排序的方式，Asc：升序，Desc：降序"),
            @ApiImplicitParam(name = "current", value = "当前页码"),
            @ApiImplicitParam(name = "size", value = "每页展示数据的条数")
    })
    @ApiResponse(code = 200, message = "成功", response = OperatorModel.class)
    @GetMapping("/pageList")
    public PageAndConditionResult pageList(PageRequest pageRequest, OperatorModel operatorModel) {
        String orderAttr = pageRequest.getOrderAttr();
        String orderType = pageRequest.getOrderType();
        Long current = pageRequest.getCurrent();
        Long size = pageRequest.getSize();
        PageResult pageResult = operatorService.pageList(orderAttr, orderType, current, size, operatorModel);

        User userAuthorization = UserAuthorization.getUserAuthorization();
        Long userId = userAuthorization.getId();
        List<TableStruct> tableField = tableStructService.getTableField(userId, "operator");

        PageAndConditionResult pageAndConditionResult = new PageAndConditionResult();
        pageAndConditionResult.setFields(tableField);
        pageAndConditionResult.setPage(pageResult);

        return pageAndConditionResult;
    }

    /**
     * 切换算法绑定的算子版本号
     */
    @ApiOperation("切换算法绑定的算子版本号")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "operatorId", value = "算子Id"),
            @ApiImplicitParam(name = "operatorVersionId", value = "算子版本Id")
    })
    @GetMapping("/changeBindingOperatorVersion")
    public ResultData changeBindingOperatorVersion(Long operatorId, Long operatorVersionId) {
        operatorService.changeBindingOperatorVersion(operatorId, operatorVersionId);
        return ResultData.success();
    }
}
